﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class GeChaoJi : Device
    {
        /// <summary>
        /// 测试模式
        /// </summary>
        public GeChaoJiEnum.TestMode mode { get; set; }
        /// <summary>
        /// 电机模式
        /// </summary>
        public GeChaoJiEnum.DianJiMode dianjimode { get; set; }
        public int 通道 { get; set; }
        public string 电平 { get; set; }
        public string 动作类型 { get; set; }
        /// <summary>
        /// 电平模式
        /// </summary>
        public GeChaoJiEnum.DianPinMode dianpinmode { get; set; }
        public override Order GetOrder(string deviceName)
        {
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case GeChaoJiEnum.TestMode.初始化:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道 });
                    break;
                case GeChaoJiEnum.TestMode.测试模式:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道});
                    break;
                case GeChaoJiEnum.TestMode.读参数:
                    order.range = GetRange("mV");
                    order.range1 = GetRange1("C");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 上限值, 下限值, 上限值1, 下限值1 });
                    break;
                case GeChaoJiEnum.TestMode.读端口:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 电平 });
                    break;
                case GeChaoJiEnum.TestMode.测试通讯端口:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道 });
                    break;
                case GeChaoJiEnum.TestMode.进入通讯端口模式:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道 });
                    break;
                case GeChaoJiEnum.TestMode.测试升降电机:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 动作类型 });
                    break;
                case GeChaoJiEnum.TestMode.测试行走电机:
                    order.range = GetRange("s");
                    order.range1 = GetRange1("s");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 动作类型, 下限值, 上限值, 上限值1, 下限值1 });
                    break;
                case GeChaoJiEnum.TestMode.测试割草电机:
                    order.range = GetRange("s");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, 动作类型, 下限值, 上限值 });
                    break;
                case GeChaoJiEnum.TestMode.测试结束:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道 });
                    break;
                default:
                    break;
            }
            return order;
        }

        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {

            var device = dev as IGeChaoJi;
            var result = new DeviceResult();
            switch (mode)
            {
                case GeChaoJiEnum.TestMode.初始化:
                    //初始化
                    var r1 = device.GetCMD_R_INIT(通道);
                    //初始化值
                    result.success = r1.IsSucceed;
                    if (!result.success) {
                        result.result = r1.Err;
                        break;
                    }
                    if (r1.Value!=1)
                    {
                        result.result = r1.Value.ToString()+"0：未准备好。1：初始化完成";
                        break;
                    }
                    result.result = "OK";
                    break;
                case GeChaoJiEnum.TestMode.测试模式:
                    //开始测试模式
                    var r2 = device.GetCMD_T_BEGIN(通道);
                    //初始化值
                    result.success = r2.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r2.Err;
                        break;
                    }
                    if (r2.Value!=1)
                    {
                        result.result = r2.Value.ToString()+"0：未准备好。1：准备完成";
                        break;
                    }
                    result.result = "OK";
                    break;
                case GeChaoJiEnum.TestMode.读参数:
                    //开始测试模式
                    var r3 = device.GetCMD_R_PARA(通道);
                    //初始化值
                    result.success = r3.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r3.Err;
                        break;
                    }
                    if (下限值 != null && result.success)
                    {
                        result.success = r3.Value[0] >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r3.Value[0] <= 上限值;
                    }
                    if (下限值1 != null && result.success)
                    {
                        result.success = r3.Value[1] >= 下限值1;
                    }
                    if (上限值1 != null && result.success)
                    {
                        result.success = r3.Value[1] <= 上限值1;
                    }
                    
                    result.result = DataConvert.ToUnit(r3.Value[0], "F3") + "mV\r\n"+DataConvert.ToUnit(r3.Value[1], "F3") + "C\r\n";
                    break;
                case GeChaoJiEnum.TestMode.读端口:
                    var r4 = device.GetCMD_R_INPUT((GeChaoJiEnum.DianPinMode)Enum.Parse(typeof(GeChaoJiEnum.DianPinMode),电平),通道);
                    //初始化值
                    result.success = r4.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r4.Err;
                        break;
                    }
                    for (int i=0;i<16;i++)
                    {
                        string bitname = "";
                        switch (i)
                        {
                            case 15:
                                bitname="T23";
                                break;
                            case 14:
                                bitname="T28";
                                break;
                            case 13:
                                bitname="T19";
                                break;
                            case 12:
                                bitname="T20";
                                break;
                            case 11:
                                bitname="T21";
                                break;
                            case 10:
                                bitname="T22";
                                break;
                            case 9:
                                bitname="J9-1";
                                break;
                            case 8:
                                bitname="J9-2";
                                break;
                            case 7:
                                bitname="JP14-6";
                                break;
                            case 6:
                                bitname="JP14-7";
                                break;
                            case 5:
                                bitname="JP14-9";
                                break;
                            case 4:
                                bitname="JP1-10";
                                break;
                            case 3:
                                bitname="Charge";
                                break;
                            case 2:
                                bitname="默认";
                                break;
                            case 1:
                                bitname="默认";
                                break;
                            case 0:
                                bitname="默认";
                                break;
                            default:
                                break;
                        }
                        if (i>13)
                        {
                            //if (r4.Value[i]!=0)
                            //{
                            //    result.success=false;
                            //    result.result = result.result+"\r\n "+bitname+"名称"+(15-i)+"号位为"+r4.Value[i];
                            //}
                            continue;
                        }
                        if (i<3)
                        {
                            if (r4.Value[i]!=0)
                            {
                                result.success=false;
                                result.result = result.result+"\r\n "+bitname+"名称"+(15-i)+"号位为"+r4.Value[i];
                            }
                            continue;
                        }
                        if (电平=="低电平")
                        {
                            if (r4.Value[i]!=0)
                            {
                                result.success=false;
                                result.result = result.result+"\r\n "+bitname+"名称"+(15-i)+"号位低电平为"+r4.Value[i];
                            }
                            continue;
                        }
                        else
                        {
                            if (r4.Value[i]!=1)
                            {
                                result.success=false;
                                result.result = result.result+"\r\n "+bitname+"名称"+(15-i)+"号位高电平为"+r4.Value[i];
                            }
                            continue;
                        }
                    }
                    break;
                case GeChaoJiEnum.TestMode.测试通讯端口:
                    var r5 = device.GetCMD_T_COMMPORT( 通道);
                    //初始化值
                    result.success = r5.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r5.Err;
                        break;
                    }
                    for (int i = 0; i<8; i++)
                    {
                        string bitname = "";
                        switch (i)
                        {
                            case 7:
                                bitname="UART4";
                                break;
                            case 6:
                                bitname="LPUART1";
                                break;
                            case 5:
                                bitname="USART1";
                                break;
                            case 4:
                                bitname="USART3";
                                break;
                            case 3:
                                bitname="UART5行走电机串口";
                                break;
                            case 2:
                                bitname="行走电机串口";
                                break;
                            case 1:
                                bitname="默认";
                                break;
                            case 0:
                                bitname="默认";
                                break;
                            default:
                                break;
                        }
                        if (i<2)
                        {
                            if (r5.Value[i]!=0)
                            {
                                result.success=false;
                                result.result = result.result+"\r\n "+bitname+"名称"+(7-i)+"号位为"+r5.Value[i];
                            }
                            continue;
                        }
                        //if (i==4)
                        //{
                        //    if (r5.Value[i]!=0)
                        //    {
                        //        result.success=false;
                        //        result.result = result.result+"\r\n "+bitname+"名称"+(7-i)+"号位为"+r5.Value[i];
                        //    }
                        //    continue;
                        //}
                        if (r5.Value[i]!=1)
                        {
                            result.success=false;
                            result.result = result.result+"\r\n "+bitname+"名称"+(7-i)+"号位为"+r5.Value[i];
                        }
                    }
                    break;
                case GeChaoJiEnum.TestMode.测试升降电机:
                    var r6 = device.GetCMD_T_LIFTMOTOR((GeChaoJiEnum.DianJiMode)Enum.Parse(typeof(GeChaoJiEnum.DianJiMode), 动作类型), 通道);
                    //初始化值
                    result.success = r6.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r6.Err;
                        break;
                    }
                    result.value = r6.Value;
                    if (动作类型=="停止"&&r6.Value!=0)
                    {
                        result.success=false;
                        result.result = $"停止命令失败，结果为{r6.Value}";
                    }
                    else if(动作类型=="正转"&&r6.Value!=1)
                    {

                        result.success=false;
                        result.result = $"正转命令失败，结果为{r6.Value}";
                    }
                    else if (动作类型=="反转"&&r6.Value!=2)
                    {

                        result.success=false;
                        result.result = $"反转命令失败，结果为{r6.Value}";
                    }
                    result.result = $"命令{动作类型},返回{r6.Value}";
                    break;
                case GeChaoJiEnum.TestMode.测试行走电机:
                    var r7 = device.GetCMD_T_WALKMOTOR((GeChaoJiEnum.DianJiMode)Enum.Parse(typeof(GeChaoJiEnum.DianJiMode), 动作类型), 通道);
                    //初始化值
                    result.success = r7.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r7.Err;
                        break;
                    }
                    if (下限值 != null && result.success)
                    {
                        result.success = r7.Value[0] >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r7.Value[0] <= 上限值;
                    }
                    if (下限值1 != null && result.success)
                    {
                        result.success = r7.Value[1] >= 下限值1;
                    }
                    if (上限值1 != null && result.success)
                    {
                        result.success = r7.Value[1] <= 上限值1;
                    }

                    result.result = "左轮"+DataConvert.ToUnit(r7.Value[0], "F3") + "\r\n"+"右轮"+DataConvert.ToUnit(r7.Value[1], "F3") + "\r\n";

                    break;
                case GeChaoJiEnum.TestMode.测试割草电机:
                    var r8 = device.GetCMD_T_MOWMOTOR((GeChaoJiEnum.DianJiMode)Enum.Parse(typeof(GeChaoJiEnum.DianJiMode), 动作类型), 通道);
                    //初始化值
                    result.success = r8.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r8.Err;
                        break;
                    }
                    if (下限值 != null && result.success)
                    {
                        result.success = r8.Value >= 下限值;
                    }
                    if (上限值 != null && result.success)
                    {
                        result.success = r8.Value <= 上限值;
                    }
                    result.value = r8.Value;
                    result.result = "割草电机转速"+DataConvert.ToUnit(r8.Value, "F3") + "mV";
                    break;
                case GeChaoJiEnum.TestMode.测试结束:
                    //测试结束
                    var r9 = device.GetCMD_T_END(通道);
                    //测试结果赋值
                    result.success = r9.IsSucceed;
                    result.result = r9.IsSucceed ? "OK" : r9.Err;
                    break;
                case GeChaoJiEnum.TestMode.进入通讯端口模式:
                    var r10 = device.GetCMD_T_COMMPORT(通道);
                    //初始化值
                    result.success = r10.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r10.Err;
                        break;
                    }
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
